home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch19 / prog3.c < prev    next >
C/C++ Source or Header  |  1994-08-09  |  5KB  |  235 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <graphics.h>
  6. #include <dos.h>
  7.  
  8.  
  9. #define PI 3.1415926
  10. #define EPSILON 1E-3
  11.  
  12.  
  13. void xrotate(float *, int);
  14. void yrotate(float *, int);
  15. void zrotate(float *, int);
  16. void getmouse_xy(int *, int *, int *);
  17.  
  18.  
  19. float st[361], ct[361];
  20. union REGS regs;
  21.  
  22.  
  23. void main(void)
  24. {
  25. int i,j,k,l;
  26. int height, height2;
  27. unsigned char x,y,z;
  28. int xx, yy, button;
  29. int tx, ty, tz;
  30. float point[3];
  31. int view[100][100];
  32. unsigned char bubblemass[70][70][70];
  33. unsigned char bubble[21][21][21];
  34. int grdriver, grmode, grerror;
  35.   
  36.   
  37.   
  38.   /*define sine and cosine tables*/
  39.   
  40.   for(i=0; i<=360; i++)
  41.     {
  42.     st[i]=(float)sin((double)(2*PI/360.0*i));
  43.     ct[i]=(float)cos((double)(2*PI/360.0*i));
  44.     }
  45.  
  46.   
  47.   /*MAKE BUBBLE*/
  48.  
  49.   for(i=-10; i < 11; i++)
  50.     {
  51.     height = (int)ceil(10.0*sin(acos((double)i/10.0)));
  52.     if(height != 0)
  53.       for(j=-height; j < height+1; j++)
  54.         {
  55.         height2 = (int)ceil(height*sin(acos((double)j/height)));  
  56.         if(height2 !=0)
  57.           for(k=-height2; k < height2+1; k++)
  58.             bubble[i+10][j+10][k+10] = 1;       
  59.         }
  60.     
  61.     }
  62.  
  63.   
  64.   /*INITIATE BUBBLE MASS*/
  65.  
  66.   for(i=0; i < 70; i++)
  67.     for(j=0; j < 70; j++)
  68.       for(k=0; k < 70; k++)
  69.         bubblemass[i][j][k] = 0;
  70.  
  71.   
  72.   /*CREATE BUBBLE MASS*/
  73.  
  74.   for(l=0; l < 100; l++)
  75.     {
  76.     x = (unsigned char)(((float)(rand())*35/(float)RAND_MAX)+10);
  77.     y = (unsigned char)(((float)(rand())*35/(float)RAND_MAX)+10);
  78.     z = (unsigned char)(((float)(rand())*35/(float)RAND_MAX)+10);
  79.  
  80.     for(i=0; i < 21; i++)
  81.       for(j=0; j < 21; j++)
  82.         for(k=0; k < 21; k++)
  83.           if(bubble[i][j][k] == 1)
  84.             if(bubblemass[x+i][y+j][z+k] < 15)
  85.               bubblemass[x+i][y+j][z+k] ++;
  86.     }
  87.  
  88.   
  89.   /*INITIALIZE GRAPHICS SCREEN*/
  90.  
  91.   grdriver = VGA256;
  92.   grmode = 0;
  93.   initgraph(&grdriver, &grmode, "");
  94.   grerror = graphresult();
  95.   if(grerror)
  96.     {
  97.     closegraph();
  98.     printf("Error Initializing Graphics Mode.\n");
  99.     exit(1);
  100.     }
  101.   
  102.   
  103.   /*MOUSE INITIALIZATION*/
  104.  
  105.   regs.w.ax = 0;
  106.   int386(0x33, ®s, ®s);
  107.   
  108.   if((short)regs.w.ax != -1)
  109.     {
  110.     closegraph();
  111.     printf("Mouse driver must be installed before running ");
  112.     printf("this program.\n");
  113.     exit(1);
  114.     }
  115.   
  116.   regs.w.ax = 0x0007;
  117.   regs.w.cx = 0;
  118.   regs.w.dx = 0x0280;
  119.   int386(0x33, ®s, ®s);
  120.   regs.w.ax = 0x0008;
  121.   regs.w.cx = 0;
  122.   regs.w.dx = 0x00c8;
  123.   int386(0x33, ®s, ®s);
  124.   regs.w.ax = 0x0001;
  125.   int386(0x33, ®s, ®s);
  126.   
  127.    
  128.   /*MIP IMAGE DISPLAY*/
  129.  
  130.   while(!kbhit())
  131.     {
  132.      
  133.     /*GET ROTATION VALUES FROM MOUSE*/
  134.  
  135.     getmouse_xy(&xx, &yy, &button);
  136.    
  137.     tx = (int)((float)(yy-100)*1.8);
  138.     ty = (int)((float)(xx-320)*0.5625);
  139.     
  140.     while((tx < 0)||(tx > 360)||(ty < 0)||(ty > 360))
  141.       {
  142.       if(tx < 0)
  143.         tx = tx + 360;
  144.       if(tx > 360)
  145.         tx = tx - 360;
  146.       if(ty < 0)
  147.         ty = ty + 360;
  148.       if(ty > 360)
  149.         ty = ty - 360;
  150.       }
  151.  
  152.     /*CLEAR PROJECTION SURFACE*/
  153.  
  154.     for(i=0; i<100; i++)
  155.      for(j=0; j<100; j++)
  156.       view[i][j] = 0;
  157.  
  158.     /*ROTATE AND CREATE MIP IMAGE*/
  159.  
  160.     for(i=0; i < 70; i++)
  161.       for(j=0; j < 70; j++)
  162.         for(k=0; k < 70; k++)
  163.           if(bubblemass[i][j][k] > 0)
  164.             {
  165.             point[0] = (float)(i - 35);
  166.             point[1] = (float)(j - 35);
  167.             point[2] = (float)(k - 35);
  168.             xrotate(point,tx);
  169.             yrotate(point,ty);
  170.             if(view[(int)(point[0]+50.0)][(int)(point[1]+50.0)] 
  171.               < bubblemass[i][j][k])
  172.               view[(int)(point[0]+50.0)][(int)(point[1]+50.0)] 
  173.                  = bubblemass[i][j][k];
  174.             }
  175.  
  176.  
  177.     /*PLOT MIP IMAGE ONTO GRAPHICS SCREEN*/
  178.  
  179.     for(i=0; i<100; i++)
  180.       for(j=0; j<100; j++)
  181.         {
  182.         putpixel(i+110, j+50, 16+view[i][j]);
  183.         }
  184.  
  185.     }
  186.   getch();
  187.   closegraph();
  188. }
  189.  
  190.  
  191. void xrotate(float *point, int deg)
  192. {
  193. float y,z;
  194.  
  195.   y = point[1]*ct[deg] + point[2]*st[deg];
  196.   z = point[2]*ct[deg] - point[1]*st[deg];
  197.  
  198.   point[1] = y;
  199.   point[2] = z;
  200.  
  201. }
  202.  
  203.  
  204. void yrotate(float *point, int deg)
  205. {
  206. float x,z;
  207.  
  208.   x = point[0]*ct[deg] - point[2]*st[deg];
  209.   z = point[0]*st[deg] + point[2]*ct[deg];
  210.   point[0] = x;
  211.   point[2] = z;
  212. }
  213.  
  214.  
  215. void zrotate(float *point, int deg)
  216. {
  217. float x,y;
  218.  
  219.   x = point[0]*ct[deg] + point[1]*st[deg];
  220.   y = point[1]*ct[deg] - point[0]*st[deg];
  221.   point[0] = x;
  222.   point[1] = y;
  223. }
  224.  
  225.  
  226.  
  227. void getmouse_xy(int *xx, int *yy, int *button)
  228. {
  229.   regs.w.ax=0x0003;
  230.   int386(0x33, ®s, ®s);
  231.   *xx=(int)regs.w.cx;
  232.   *yy=(int)regs.w.dx;
  233.   *button=regs.w.bx;
  234. }
  235.